package live.zs.prometheusexample.metrics;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author madison
 * @description
 */
@Component
public class ScheduledDemo {

    private Logger logger = LoggerFactory.getLogger(ScheduledDemo.class);

    @Autowired
    private CharlesMetrics charlesMetrics;

    private int count1;
    private int count2;
    private int count3;

    @Async("One")
    @Scheduled(fixedDelay = 5000)
    public void increment1() {
        count1++;
        charlesMetrics.counter1.increment();
        // 将counter1的值放到Gauge中，反应应用的当前指数
        charlesMetrics.map.put("x", Double.valueOf(count1));
        logger.info("increment1 count: {}", count1);
    }

    @Async("Two")
    @Scheduled(fixedDelay = 10000)
    public void increment2() {
        count2++;
        charlesMetrics.counter2.increment();
        logger.info("increment2 count: {}", count2);
    }

    @Async("Three")
    @Scheduled(fixedDelay = 20000)
    public void increment3() {
        count3++;
        charlesMetrics.counter3.increment();
        logger.info("increment3 count: {}", count3);
    }
}
